const vscode = require('vscode');
module.exports = function (context) {
  context.subscriptions.push(vscode.commands.registerCommand('extension.createfiles', () => {
    // vscode.window.showInformationMessage(`你点我干啥，我长得很帅吗？`);
    console.log('测试激活时间');
    vscode.window.showInformationMessage(`测试激活时间!`);

    // ok这里触发 事件
  }));
  // extension.createfiles1
  context.subscriptions.push(vscode.commands.registerCommand('extension.createfiles1', (res) => {
    console.log('触发文件生成命令', res);
    // console.log(res["path"]);
    // console.log(vscode.WorkspaceEdit.createFile, context);
    console.log('分隔符');
    console.log('获取当前编辑器:', );
    // console.log('1开始生成2:');
    let files_arr = ["index.html", "index.js", "index.less", "index.css"]
    try {
      // console.log('开始生成2:');
      if (!!res) {

        console.log('workspaceEdit.createFile', );
        for (let i = 0; i < files_arr.length; i++) {
          let workspaceEdit = new vscode.WorkspaceEdit()
          let uri = res["path"] + "/" + files_arr[i]
          let vscode_uri = vscode.Uri.file(uri)
          workspaceEdit.createFile(vscode_uri)
          vscode.workspace.applyEdit(workspaceEdit)
        }
        // console.log('获取结果:', workeEdit);
        vscode.window.showInformationMessage(`文件创建成功!`);
      } else {
        // console.log('此处写入内容!');
        // 但是获取不到对应的路径是个问题 从快捷键触发
        // if (!!) {
        //   // vscode.window.showInformationMessage();
        vscode.window.showErrorMessage(`选择文件夹后重试!`)
        // }

      }
    } catch (error) {
      console.log('失败返回:', error);
      vscode.window.showErrorMessage(`文件创建失败!`);
    }
    // } else {
    //   vscode.window.showInformationMessage(`选择文件夹后重试!`);
    // }
    // ok这里触发 事件
  }));
  // context.subscriptions.push(vscode.commands.registerCommand('createfiles2', (res) => {
  //   console.log('获取createfiles2的触发?', res);
  // }));
  function createFiles(type, paths, folder, filename = "", cssType = "less") {
    let files_arr = ["index.vue"]
    try {
      // if (!!res) {
      console.log('workspaceEdit.createFile2', paths, folder);
      // for (let i = 0; i < files_arr.length; i++) {
      let workspaceEdit = new vscode.WorkspaceEdit()
      let uri = ""
      console.log('判断失败?', !filename);
      if (!filename) {
        uri = paths + "/" + folder + "/" + files_arr[0]
      } else {
        uri = paths + "/" + folder + "/" + filename
      }
      let vscode_uri = vscode.Uri.file(uri)
      // insert
      workspaceEdit.createFile(vscode_uri)
      if (!workspaceEdit.has(vscode_uri)) {
        let position = new vscode.Position(0, 0)
        let strs = require("./api_boder.js")
        console.log('输出获取的js的组内容:', strs);
        if (type == "vue") {
          let l_result = strs["vue_txt"].replace(/#0/g, cssType).replace(/#1/g, filename || folder)
          console.log('strs["vue_txt"]', l_result);
          workspaceEdit.insert(vscode_uri, position, l_result)
        }
        if (type == "uniapp") {
          let l_result = strs["uniapp_txt"].replace(/#0/g, cssType).replace(/#1/g, filename || folder)
          workspaceEdit.insert(vscode_uri, position, l_result)
        }
      }
      vscode.workspace.applyEdit(workspaceEdit)
      // }
      vscode.window.showInformationMessage(`文件创建成功!`);

    } catch (error) {
      console.log('失败返回:', error);
      vscode.window.showErrorMessage(`文件创建失败!`);
    }
  }
  context.subscriptions.push(vscode.commands.registerCommand('createfiles2', (res) => {
    console.log('获取createfiles2的触发?', res);
    // scss
    openPopup("vue", res)
  }));

  function openPopup(types, res) {
    if (!!res) {
      try {
        vscode.window.showInputBox({ // 这个对象中所有参数都是可选参数
          password: false, // 输入内容是否是密码
          title: "创建文件",
          ignoreFocusOut: false, // 默认false，设置为true时鼠标点击别的地方输入框不会消失
          placeHolder: '请输入生成文件名？', // 在输入框内的提示信息
          prompt: '输入对应的文件夹|文件名|cssType', // 在输入框下方的提示信息
          // validateInput: function (text) {
          //   console.log('校验:', text);
          //   return text;
          // } // 对输入内容进行验证并返回
        }).then(function (filePath) {
          // res["path"] 路径 文件夹 filePath 文件|文件名
          if (filePath.indexOf("|") > -1) {
            // createFiles
            // filename
            paths = res["path"]
            folder = filePath.split("|")[0]
            filename = filePath.split("|")[1]
            cssType = filePath.split("|")[2]
            if (filename.length > 0 && filename.indexOf("vue") == -1) {
              filename = filename + ".vue"
            }
            // +".vue"
            createFiles(types, paths, folder, filename, cssType)
          } else {
            paths = res["path"]
            // + "/" + createFiles
            folder = filePath
            // /others
            // filename = "lens"
            createFiles(types, paths, folder)
          }
          console.log("用户输入：", filePath);
        }).catch(err => {
          console.log('错误了?', err);
        })
      } catch (error) {
        console.log('失败返回:', error);
        vscode.window.showErrorMessage(`文件创建失败!`);
      }
    } else {
      vscode.window.showErrorMessage(`选择文件夹后重试!`)
    }
  }
  context.subscriptions.push(vscode.commands.registerCommand('createfiles3', (res) => {
    console.log('获取createfiles3的触发?', res);
    // less
    // openPopup(res)
    openPopup("uniapp", res)
    // window.createInputBox()
    // prompt?
    // inputboxs.prompt = ""
    // inputboxs.showInputBox
    // res["path"]
    // let inputOptions = vscode.InputBoxOptions({
    //   prompt: "测试input框"
    // })
    // window.qu
    // console.log('设置 options:', inputOptions);
    // window.showInputBox()
    // let backtxt = window.createOutputChannel("输入创建的文件")
    // console.log('测试生成inputboxs:', inputboxs, backtxt);
    // createFiles("less", res)
  }));
  // registerCommand
  // createfiles2
};